//
//  LcNetworkCenter.h
//  18Birdies
//
//  Created by Lc on 15/3/20.
//  Copyright (c) 2015年 Lc. All rights reserved.
//

#import <UIKit/UIKit.h>

#import "LcSingleton.h"

typedef void (^ImageDownloadHandler)(NSString* url, UIImage* image);
typedef void (^FileDownloadHandler)(NSString* url, BOOL success);
typedef void (^ResultHandler)(BOOL success);
typedef void (^ResultDataHandler)(BOOL success, id auxiliary);

@interface LcNetworkCenter : LcSingleton

@property(nonatomic, strong, readonly) NSString* multipartBoundary;

/*
 Caution: completionBlock is not excuted on the caller thread
 */
- (void)httpGet:(NSString*)urlString
      withParameters:(NSDictionary*)parameters
             headers:(NSDictionary*)headers
    completionHandler:(void (^)(NSHTTPURLResponse* response,
                               NSData* responseBody,
                               NSError* error))completionHandler;
/*
 Caution: completionBlock is not excuted on the caller thread
 */
- (void)httpPost:(NSString*)urlString
        withBody:(NSData*)body
         headers:(NSDictionary*)headers
completionHandler:(void (^)(NSHTTPURLResponse* response,
                           NSData* responseBody,
                           NSError* error))completionHandler;

- (NSData*)createMultipartBodyWithStrings:(NSDictionary*)stringParts
                                 andFiles:(NSDictionary*)fileParts;
- (NSData*)createMultipartBodyWithStrings:(NSDictionary*)stringParts
                                 andDatas:(NSDictionary*)dataParts;

- (void)downloadImage:(NSString*)url
           saveToFile:(NSString*)fileName
      completeHandler:(ImageDownloadHandler)completeHandler;
- (void)downloadFile:(NSString*)url
          saveToFile:(NSString*)fileName
     completeHandler:(FileDownloadHandler)completeHandler;

// visible for test
- (NSString*)urlString:(NSString*)url withParameters:(NSDictionary*)parameters;
/*
 Caution: completionBlock is not excuted on the caller thread
 */
- (void)httpPost:(NSString*)urlString
     withStrings:(NSDictionary*)stringParts
           files:(NSDictionary*)fileParts
         headers:(NSDictionary*)headers
completionHandler:(void (^)(NSHTTPURLResponse* response,
                           NSData* responseBody,
                           NSError* error))completionHandler;
- (void)blockUntilFinished;

@end
